package com.stx.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.stx.entity.User;
import com.stx.service.UserService;
import com.stx.util.MD5_Test;

public class AdminUser extends ActionSupport{

	private UserService us;
	private List<User> userList=new ArrayList<User>();
	private int  allPage;
	private int  offsetPage;
	private User user;
	private String date;
    private String email;
    private String password;
    private String us_type;
    
    
	public String getUs_type() {
		return us_type;
	}
	public void setUs_type(String us_type) {
		this.us_type = us_type;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAllPage() {
		return allPage;
	}
	public void setAllPage(int allPage) {
		this.allPage = allPage;
	}
	public int getOffsetPage() {
		return offsetPage;
	}
	public void setOffsetPage(int offsetPage) {
		this.offsetPage = offsetPage;
	}
	public UserService getUs() {
		return us;
	}
	public void setUs(UserService us) {
		this.us = us;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
	/*
	 * 影视列表的到一个分页每页显示10个
	 * 需要简单计算下总的条数 分页显示风格是什么样的
	 * 需要第一个查询点 offsetPage allPage
	 * 得到总的信息条数
	 * 第一次offsetPage=0;offst的默认的是0
	 * 显示的时候根据allPage判定是否要下一页
	 * */
	public String findAllUser(){
		
		int countPage=10;
		int count=us.getCountUserList();
		if(count%10==0){
			allPage=count/10;
		}else{
			 allPage=count/10+1;
		}
		userList=us.getAllUser(offsetPage*countPage,countPage);
		for(int i=0;i<userList.size();i++){
			System.out.println(userList.get(i).getUs_id());
		}
		return "success";
	}
	
	/*
	 * 得到用户的所有信息到用户管理中心
	 * 修改了显示的时间格式
	 * email换成了一个变量，检测是否email是否更新了
	 * 判断img是否为空
	 * */
	public String execute() {
		
		user=us.getUserInformation(user);
		if(user!=null){
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        date=sdf.format(user.getUs_createtime());
	        email=user.getUs_email();
	        us_type=String.valueOf(user.getUs_type());
	        if(user.getUs_image()==null||user.getUs_image().equals("")){
	        	user.setUs_image("images/user_normal.jpg");
	        }
	        System.out.println(user.getUs_image());
		return "success";
		}else{
			addActionError("出错了");
			return "failure";
		}
	}
	/*
	 * 更新用户的操作
	 * 首先判断下是否用户修改了email
	 * 如果更换邮箱先检查下是否被使用
	 * */
	public String upadteUser(){
		
		User u=us.getUserInformation(user);
		/*检查下u是否有效*/
		if(u!=null){
		user.setUs_password(u.getUs_password());
		user.setUs_email(email);
		user.setUs_id(u.getUs_id());
		user.setUs_type(Integer.parseInt(us_type));
		user.setUs_image(u.getUs_image());
		}else{
			addActionError("出错了");
			return "sessionerror";
		}
		/*判断是否邮箱被修改了*/
		if(!email.equals(u.getUs_email())&&(us.getUser(user).size()!=0)){
			addActionError("此邮箱已被使用了");
				return "failure";
			}else{
				us.updateAdminUser(user);
				addActionError("修改成功");
				/*
				 * 重新得打user然胡重置session
				 * */
				return "success";
			}
	}
	/*
	 * 修改用户的密码
	 * 首先检查原始密码是否正确
	 * */
	public String updatePassword(){
		User u=us.getUserInformation(user);
		/*检查下session是否有效*/
		if(u!=null){
		user.setUs_nickname(u.getUs_nickname());
		user.setUs_email(u.getUs_email());
		user.setUs_image(u.getUs_image());
		user.setUs_id(u.getUs_id());
		user.setUs_type(u.getUs_type());
		}else{
			addActionError("出错了");
			return "sessionerror";
		}
		/*比对下密码是否输入有误*/
		
			user.setUs_password(MD5_Test.MD5(user.getUs_password()));
			us.updateAdminUser(user);
			email=user.getUs_email();
			addActionError("密码修改成功");
			return "success";
	}
	/*
	 * 删除给用户
	 * */
	public String delUser(){
		user=us.getUserInformation(user);
		us.delUserInformation(user);
		addActionError("删除用户成功");
		return "success";
	}
}
